// miniprogram/pages/payRecord/payRecord.js
const db = wx.cloud.database()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    user: '',
    showBottom: false, // 显示底部状态栏
    total: '', // 总订单数
    pageIndex: 1, // 当前页面数
    orders: [], //订单数据汇总
    perPage: 5 //每页显示订单数
  },

  /**
   * 
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //----------------------------------1.获取用户信息
    let user = JSON.parse(options.user)
    //console.log(options)
    this.setData({
      user: user
    })
    //----------------------------------2.设置会员卡号
    let barTitle = '消费记录'
    if(user.cardid.length > 0){
      barTitle = user.cardid
    }
    wx.setNavigationBarTitle({
      title: barTitle
    })
    //----------------------------------3.刷新用户订单总数
    this.getUserConsumeCount()
    //----------------------------------4.获取订单信息
    this.getOrderInfoByIndex(this.data.pageIndex)
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.toggleBottomStatus(true)
    let orders = this.data.orders
    let total = this.data.total
    let index = this.data.pageIndex

    //如果不是尾页，则需要加载数据
    if (orders.length < total) {
      this.getUserInfoByIndex(index + 1)
    }
  },
  /**
 * 更多点击事件
 */
  moreClick: function () {
    wx.showToast({
      title: '开发中...',
      icon: 'none'
    })
  },

  //------------------------------------------查询该用户一共消费了几笔订单

  getUserConsumeCount: function () {
    let that = this
    let user = this.data.user
    db.collection('orders').where({ userId: user.userid }).count()
      .then(res => {
        that.setData({ total: res.total })
      })
  },
  //------------------------------------------根据页码获取用户信息
  getOrderInfoByIndex: function (index) {
    let that = this
    let user = this.data.user
    let limit = this.data.perPage
    let skip = limit * (index - 1)
    let orders = this.data.orders
    console.log('skip:', skip, 'limit:', limit, 'orders:', orders)
    db.collection('orders')
      .where({ userId: user.userid}) //筛选当前用户
      .skip(skip) // 跳过结果集中的xx条
      .limit(limit) // 限制返回数量为xx条
      .get()
      .then(res => {
        console.log(res)
        for (let i = 0; i < res.data.length; i++) {
          orders.push(res.data[i])
        }
        that.setData({
          orders: orders,
          pageIndex: index
        })
        that.toggleBottomStatus(false)
      })
  },
  /**
 * 页面上拉触底事件的处理函数
 */
  onReachBottom: function () {
    this.toggleBottomStatus(true)
    let orders = this.data.orders
    let total = this.data.total
    let index = this.data.pageIndex

    //如果不是尾页，则需要加载数据
    if (orders.length < total) {
      this.getOrderInfoByIndex(index + 1)
    }

  },
  toggleBottomStatus: function (bool) {
    this.setData({
      showBottom: bool
    })
  },
//------------------------------------------查看订单详情
  checkOrderDetail:function(e){
    getApp().globalData.orderDetail = e.currentTarget.dataset.order
    wx.navigateTo({
      url: '../../pages/orderDetail/orderDetail',
    })
  }
})